package com.jye.rseven.user.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.jye.rseven.Constants.MyConstants;
import com.jye.rseven.domain.JsonResult;
import com.jye.rseven.domain.PtGoods;
import com.jye.rseven.domain.TyPtCode;
import com.jye.rseven.service.PtGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/ptGoods")
@Api(tags="拼团商品API")
public class PtGoodsController {

    @Reference
    PtGoodsService ptGoodsService;

    @PostMapping("/addPtGoods")
    @ApiOperation(value="添加拼团商品", notes="根据拼团商品实体添加实体", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "PtGoods",
                    value = "商品实体类", required = true,
                    dataType = "com.jye.rseven.user.domain.PtGoods"),
    })
    public JsonResult addPtGoods(PtGoods ptGoods) {
        JsonResult jsonResult = new JsonResult();
        int i = ptGoodsService.addPtGoods(ptGoods);
        if (i == 1) {
            jsonResult.setData("添加成功!");

        } else {
            jsonResult.setData("添加失败!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }

    @GetMapping("/findPtGoods")
    @ApiOperation(value="查看商品", notes="查看所有拼团商品", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "skip",
                    value = "跳过多少条", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "limit",
                    value = "限制多少条", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findPtGoods(int skip, int limit){
        JsonResult jsonResult = new JsonResult();
        List<PtGoods> list = ptGoodsService.findPtGoods(skip,limit);
        if (list.size()!=0 ){
            jsonResult.setData(list);
        }else{
            jsonResult.setCode("500");
            jsonResult.setMsg("未查询到记录，查询失败");
        }
        return jsonResult;
    }

    @GetMapping("/findPtGoodsById")
    @ApiOperation(value="查找单个商品", notes="查询单个商品", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "Id",
                    value = "商品id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findPtById(int Id){
        JsonResult jsonResult = new JsonResult();
        PtGoods ptGoods = ptGoodsService.findPtGodsById(Id);
        if (ptGoods !=null ){
            jsonResult.setData(ptGoods);
        }else{
            jsonResult.setCode("500");
            jsonResult.setMsg("未查询到记录，查询失败");
        }
        return jsonResult;
    }

    @GetMapping("/findPtGoodsByName")
    @ApiOperation(value="模糊查询商品", notes="模糊查询商品", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "ptGoodsName",
                    value = "商品名称", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findPtGoodsByName(String ptGoodsName){
        JsonResult jsonResult = new JsonResult();
        List<PtGoods> list = ptGoodsService.findPtGoodsByName(ptGoodsName);
        if (list.size() !=0 ){
            jsonResult.setData(list);
        }else{
            jsonResult.setCode("500");
            jsonResult.setMsg("未查询到记录，查询失败");
        }
        return jsonResult;
    }

}
